package com.itheima.wms.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.itheima.wms.controller.dto.LoginRequest;
import com.itheima.wms.controller.vo.UserInfoVo;
import com.itheima.wms.mapper.LoginMapper;
import com.itheima.wms.service.LoginService;
import com.itheima.wms.utils.JwtUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * @author Administrator
 */
@Service
@RequiredArgsConstructor
public class LoginServiceImpl implements LoginService {
    private final LoginMapper userMapper;

    @Override
    public UserInfoVo login(LoginRequest request) {
        UserInfoVo user = userMapper.login(request.getUsername(), request.getPassword());
        if (user == null) {
            return null;
        }
        UserInfoVo userInfoVo = new UserInfoVo(user.getId(),user.getUsername(),user.getName(),user.getAvatar(),user.getToken());
        // 登陆成功：生成令牌（TOKEN）
        // 生成令牌
        Map<String, Object> claims = BeanUtil.beanToMap(userInfoVo);
        String jwt = JwtUtil.createJwt(claims);
        userInfoVo.setToken(jwt);
        return  userInfoVo;
    }
}
